﻿using System.Collections.Generic;

namespace ProblemsSet
{
    public class Problem_41 : BaseProblem
    {
        public override object GetResult()
        {
            return GetDecision2();
        }

        private static long GetDecision()
        {
            var vals = MathLogic.GetPrimeList(10000000, true);
            for (var i = vals.Count - 1; i >= 0; i--)
            {
                if (MathLogic.GetCountOfDigits(vals[i]) != MathLogic.GetDigitSet(vals[i]).Count) continue;
                if (MathLogic.GetDigitSet(vals[i]).Contains(0)) continue;
                bool ex = true;
                for (var vl = 1; vl <= MathLogic.GetCountOfDigits(vals[i]); vl++)
                {
                    if (!MathLogic.GetDigitSet(vals[i]).Contains((byte)vl))
                    {
                        ex = false;
                        break;
                    }
                }
                if (!ex) continue;
                return vals[i];
            }
            return -1;
        }

        private static long GetDecision2()
        {
            var res = new List<string>();
            MathLogic.FormeString(ref res, "", "1234567", int.MaxValue);
            for (var i = res.Count-1 ; i>=0; i--)
            {
                var s = res[i];
                if (s[6] == '0') continue;
                if (s[6] == '2') continue;
                if (s[6] == '4') continue;
                if (s[6] == '5') continue;
                if (s[6] == '6') continue;
                if (s[6] == '8') continue;
                long q = 0;
                if (!long.TryParse(s, out q)) continue;
                if (MathLogic.IsPrimeNumber(q)) return q;
            }
            res = new List<string>();
            MathLogic.FormeString(ref res, "", "1234", int.MaxValue);
            for (var i = res.Count - 1; i >= 0; i--)
            {
                var s = res[i];
                if (s[6] == '0') continue;
                if (s[6] == '2') continue;
                if (s[6] == '4') continue;
                if (s[6] == '5') continue;
                if (s[6] == '6') continue;
                if (s[6] == '8') continue;
                long q = 0;
                if (!long.TryParse(s, out q)) continue;
                if (MathLogic.IsPrimeNumber(q)) return q;
            }
            return -1;
        }


        public override string Problem
        {
            get
            {
                return @"";
            }
        }

        public override bool IsSolved
        {
            get
            {
                return true;
            }
        }

        public override object Answer
        {
            get
            {
                return 7652413;
            }
        }
    }
}
